﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace WPFStudyAnimation
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            DoubleAnimation animation = new DoubleAnimation();
            //animation.From = btn.Width; //设置动画初始值
            //animation.To = btn.Width - 20; //设置动画结束值
            animation.By = -30;
            animation.Duration = TimeSpan.FromSeconds(0.1); //设置动画持续时间
            animation.AutoReverse = true; //是否往返执行
            animation.RepeatBehavior = new RepeatBehavior(9); //执行周期

            animation.Changed += Animation_Changed;
            animation.Completed += Animation_Completed;

            //在当前按钮的width属性实现动画
            btn.BeginAnimation(Button.WidthProperty, animation);
        }

        private void Animation_Changed(object? sender, EventArgs e)
        {
            btn.Content = "Animation Changed";
        }

        private void Animation_Completed(object? sender, EventArgs e)
        {
            btn.Content = "Animation Completed";
        }
    }
}